/*
 * @Author: wurangkun wurangkun@qq.com
 * @Date: 2023-07-07 09:09:01
 * @LastEditors: wurangkun wurangkun@qq.com
 * @LastEditTime: 2023-07-07 12:14:07
 * @FilePath: \reset_sever\module\count_down.js
 * @Description: 倒计时关机
 */
const fs = require('fs')
const path = require('path');
const { shutdown } = require('./shutdown');
const { hour } = require('../config');
const filePath = path.join(__dirname, './data/time.text');
let count = +fs.readFileSync(filePath).toString('utf-8')
const total = hour * 60 * 60
let timer = null
fs.writeFileSync(filePath, process.pid + '')
const count_down = () => {
  fs.writeFileSync(filePath, (++count) + '')
  console.log(count, 'count');
  if (count >= total) {
    shutdown()
  } else {
    timer = setInterval(() => {
      console.log(count);
      if (count >= total) {
        shutdown()
        clearInterval(timer)
      } else {
        fs.writeFileSync(filePath, (++count) + '')
      }
    }, 1000)
  }
}

module.exports = { count_down }